<?php

namespace app\index\controller;

use service\FeishuService;
use service\GuardService;
use service\LadderService;
use think\Controller;
use think\Cache;
use think\Db;
use think\Exception;

class Common extends Controller
{
    public function index(){
        exit;
        set_time_limit(0);
        //$ctime = time();
        //FeishuService::instance()->GetchildrendepByfdepid(10000000,$ctime);exit;
        //FeishuService::instance()->GetchildrendepByfdepid(94001249,$ctime);exit;
        //$objPHPExcel = new \PHPExcel();
        $exclePath = "dep11.xlsx";
        $filename = ROOT_PATH . 'public' . DS . $exclePath;
        //dump($filename);exit;
        $extension = strtolower( pathinfo($filename, PATHINFO_EXTENSION) );
        if ($extension =='xlsx') {
            $objReader = new \PHPExcel_Reader_Excel2007();
            $objExcel = $objReader ->load($filename);
        } else if ($extension =='xls') {
            $objReader = new \PHPExcel_Reader_Excel5();
            $objExcel = $objReader->load($filename);
        }
        $excel_array=$objExcel->getsheet(0)->toArray();
        //dump($excel_array);exit;
        unset($excel_array[0]);
        unset($excel_array[1]);
        //dump($excel_array);exit;
        //603 = 598 + 5
        //$special_depids = [1,2,3,10000000,99990176,99900001,94001249];
        $special_depids = [1,2,3,99990176,99900001];
        $depids = array_unique(array_column($excel_array,0));
        $depexcel_array = array_column($excel_array,null,0);
        //dump($depexcel_array);exit;
        //dump($depids);
        $depids = array_diff($depids,$special_depids);
        dump($depids);
        //$guard_arr = [];
        $where = [];
        $where['department_id'] = ['in',$depids];
        $where['status'] = 1;
        $where['is_jtzb'] = 1;
        //$where['update_state'] = 1;
        $where['update_guard_state'] = 1;
        //$where['update_time'] = 1715848208;
        $deplists = Db::name("department")->where($where)->select();
        unset($where['department_id']);
        $deplists1 = Db::name("department")->where($where)->select();
        //dump($deplists);
        //dump($deplists1);
        $deplistids = array_column($deplists,"department_id");
        $deplistids1 = array_column($deplists1,"department_id");
        //dump($deplistids);
        //dump($deplistids1);
        //$guard_ids = array_diff($depids,$deplistids);
        $guard_ids = array_diff($depids,$deplistids);
        dump($guard_ids);exit;
        $where = [];
        $where['department_id'] = ['in',$guard_ids];
        $depnewlists = Db::name("department")->where($where)->select();
        dump($depnewlists);exit;
        foreach ($depnewlists as $depnewlist){

        }

        foreach ($excel_array as $row){
            //dump($row);exit;
            if(!in_array($row[0],$special_depids)) {
                $where = [];
                $where['department_id'] = $row[0];
                $where['status'] = 1;
                $where['is_jtzb'] = 1;
                $where['update_guard_state'] = 1;
                $where['update_time'] = 1715758586;
                $depinfo = Db::name("department")->where($where)->find();
                if (empty($depinfo)) {
                    //dump($row);
                    $guard_arr[] = $row;
                }
            }
        }
        dump($guard_arr);
    }

    public function user(){
        exit;
        set_time_limit(0);
        //$ctime = time();
        //FeishuService::instance()->GetchildrendepByfdepid(10000000,$ctime);exit;
        //FeishuService::instance()->GetchildrendepByfdepid(94001249,$ctime);exit;
        //$objPHPExcel = new \PHPExcel();
        $exclePath = "user14.xlsx";
        $filename = ROOT_PATH . 'public' . DS . $exclePath;
        //dump($filename);exit;
        $extension = strtolower( pathinfo($filename, PATHINFO_EXTENSION) );
        if ($extension =='xlsx') {
            $objReader = new \PHPExcel_Reader_Excel2007();
            $objExcel = $objReader ->load($filename);
        } else if ($extension =='xls') {
            $objReader = new \PHPExcel_Reader_Excel5();
            $objExcel = $objReader->load($filename);
        }
        $excel_array=$objExcel->getsheet(0)->toArray();
        //dump($excel_array);exit;
        unset($excel_array[0]);
        unset($excel_array[1]);
        //dump($excel_array);exit;
        //$special_depids = [1,2,3,99990176,99900001];
        $special1_depids = [1];
        $special2_depids = [2];
        $special3_depids = [3];
        $specialuser1 = [];
        $specialuser2 = [];
        $specialuser3 = [];
        $user = [];
        foreach ($excel_array as $row){
            if(in_array($row[2],$special1_depids)){
                $specialuser1[] = $row;
            }
            elseif(in_array($row[2],$special2_depids)){
                $specialuser2[] = $row;
            }
            elseif(in_array($row[2],$special3_depids)){
                $specialuser3[] = $row;
            }
            else{
                $user[] = $row;
            }
        }
        /*dump($specialuser1);
        dump($specialuser2);
        dump($specialuser3);
        dump($user);exit;*/
        //1330 = 1308 + 22
        //1340 = 1318 + 22
        //1415 - 1 - 20 - 1 = 1393
        /*$specialuser_id1 = array_diff(array_column($specialuser1,"0"),["10010"]);
        dump($specialuser_id1);*/
        $userids = array_column($user,"0");
        dump($userids);
        $where = [];
        $where['is_jtzb'] = 1;
        $where['status'] = 1;
        $where['update_guard_state'] = 1;
        $where['user_id'] = ['in',$userids];
        $userlists = db("user")->where($where)->select();
        //unset($where['user_id']);
        //$userlists1 = db("user")->where($where)->select();
        /*foreach ($userlists as $row) {
            $res = GuardService::instance()->DelPerson($row['user_id']);
            dump($res);
        }*/
        $userlistids = array_column($userlists,"user_id");
        //$userlistids1 = array_column($userlists1,"user_id");
        //dump(array_diff($userlistids1,$userlistids));exit;
        dump($userlistids);
        $usernoids = array_diff($userids,$userlistids);
        dump($usernoids);exit;

        $where = [];
        //$where['is_jtzb'] = 1;
        //$where['status'] = 2;
        //$where['update_guard_state'] = 1;
        $where['user_id'] = ['in',$usernoids];
        $usernolists = db("user")->where($where)->select();
        foreach ($usernolists as $row) {
            $res = GuardService::instance()->DelPerson($row['user_id']);
            dump($res);
        }
        dump($usernolists);exit;


        dump($userlists);exit;


        exit;
        $depids = array_unique(array_column($excel_array,0));
        $depexcel_array = array_column($excel_array,null,0);
        //dump($depexcel_array);exit;
        //dump($depids);
        $depids = array_diff($depids,$special_depids);
        //dump($depids);exit;
        //$guard_arr = [];
        $where = [];
        $where['department_id'] = ['in',$depids];
        $where['status'] = 1;
        $where['is_jtzb'] = 1;
        $where['update_state'] = 1;
        $where['update_guard_state'] = 1;
        $where['update_time'] = 1715848208;
        $deplists = Db::name("department")->where($where)->select();
        $deplistids = array_column($deplists,"department_id");
        $guard_ids = array_diff($depids,$deplistids);
        //dump($guard_ids);exit;
        $where = [];
        $where['department_id'] = ['in',$guard_ids];
        $depnewlists = Db::name("department")->where($where)->select();
        dump($depnewlists);exit;
        foreach ($depnewlists as $depnewlist){

        }

        foreach ($excel_array as $row){
            //dump($row);exit;
            if(!in_array($row[0],$special_depids)) {
                $where = [];
                $where['department_id'] = $row[0];
                $where['status'] = 1;
                $where['is_jtzb'] = 1;
                $where['update_guard_state'] = 1;
                $where['update_time'] = 1715758586;
                $depinfo = Db::name("department")->where($where)->find();
                if (empty($depinfo)) {
                    //dump($row);
                    $guard_arr[] = $row;
                }
            }
        }
        dump($guard_arr);
    }
}